package com.bosssoft.ecds.controller;

/**
 * @author 张东海
 * @date 2020/8/10
 * @description 单位票据申领记录Controller，提供了基本的增删改查，
 *               同时提供了票据申领记录向财政端的提交，获取指定ID的
 *               申领记录下的申请票据具体信息的功能
 */

import com.bosssoft.ecds.common.response.CommonCode;
import com.bosssoft.ecds.common.response.QueryResponseResult;
import com.bosssoft.ecds.common.response.ResponseResult;
import com.bosssoft.ecds.util.EntityUtil;
import com.bosssoft.ecds.entity.dto.AutoApplyDTO;
import com.bosssoft.ecds.entity.dto.AutoApplyReceiveDTO;
import com.bosssoft.ecds.entity.dto.QueryDTO;
import com.bosssoft.ecds.entity.dto.ReceiveFinanceapplyDto;
import com.bosssoft.ecds.entity.po.UbeStockFinanceapplyPO;
import com.bosssoft.ecds.entity.vo.UbeStockFinanceapplyItemVO;
import com.bosssoft.ecds.entity.vo.UbeStockFinanceapplyVO;
import com.bosssoft.ecds.service.RemoteFinanceService;
import com.bosssoft.ecds.service.RemotePoolService;
import com.bosssoft.ecds.service.impl.UbeStockFinanceapplyServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class UbeStockFinanceapplyController {

    @Resource
    UbeStockFinanceapplyServiceImpl ubeStockFinanceapplyService;

    @Resource
    RemoteFinanceService remoteFinanceService;

    @Resource
    RemotePoolService remotePoolService;

    @GetMapping("ubeStockFinanceapply")
    public QueryResponseResult getById(BigInteger id) {
        UbeStockFinanceapplyPO financeapplyPO = ubeStockFinanceapplyService.getByPrimaryKey(id);
        return new QueryResponseResult(CommonCode.SUCCESS,financeapplyPO);
    }

    @GetMapping("ubeStockFinanceapply/getAll")
    public QueryResponseResult getAll(QueryDTO query) {
        List<UbeStockFinanceapplyVO> list = ubeStockFinanceapplyService.getAllDetail(query);
        return new QueryResponseResult(CommonCode.SUCCESS, list);
    }

    @PostMapping("ubeStockFinanceapply")
    public ResponseResult create(UbeStockFinanceapplyVO ubeStockFinanceapply) {
        UbeStockFinanceapplyPO po = new UbeStockFinanceapplyPO();
        EntityUtil.VOToPO(ubeStockFinanceapply, po);
        int insert = ubeStockFinanceapplyService.insert(po);
        if(insert > 0){
            return ResponseResult.SUCCESS();
        }
        else {
            return ResponseResult.FAIL();
        }

    }

    @PutMapping("ubeStockFinanceapply")
    public ResponseResult update(UbeStockFinanceapplyVO ubeStockFinanceapplyVO) {
        UbeStockFinanceapplyPO po = new UbeStockFinanceapplyPO();
        EntityUtil.VOToPO(ubeStockFinanceapplyVO, po);
        ubeStockFinanceapplyService.update(po);
        return ResponseResult.SUCCESS();
    }

    @PutMapping("ubeStockFinanceapply/submit")
    public ResponseResult submit(UbeStockFinanceapplyVO ubeStockFinanceapplyVO) {
        ubeStockFinanceapplyVO.setStatus(1);
        UbeStockFinanceapplyPO po = new UbeStockFinanceapplyPO();
        EntityUtil.VOToPO(ubeStockFinanceapplyVO, po);
        po.setFSubmitDate(LocalDateTime.now());
        po.setFChangeSitu(null);
        ubeStockFinanceapplyService.submit(ubeStockFinanceapplyVO);
        ubeStockFinanceapplyService.update(po);
        return ResponseResult.SUCCESS();
    }

    @DeleteMapping("ubeStockFinanceapply")
    public ResponseResult delete(BigInteger id) {
        ubeStockFinanceapplyService.delete(id);
        return ResponseResult.SUCCESS();
    }

    @GetMapping("ubeStockFinanceapply/items")
    public QueryResponseResult getItems(BigInteger id) {
        List<UbeStockFinanceapplyItemVO> items = ubeStockFinanceapplyService.getItems(id);
        return new QueryResponseResult(CommonCode.SUCCESS,items);
    }

    @PostMapping("ubeStockFinanceapply/outBills")
    public QueryResponseResult testChongyang(@RequestBody List<ReceiveFinanceapplyDto> receiveDtos){
        return remoteFinanceService.outBills(receiveDtos);
    }

    @GetMapping("ubeStockFinanceapply/maxNo")
    public QueryResponseResult getMaxNo(){
        BigInteger maxNo = ubeStockFinanceapplyService.getMaxNo();
        return new QueryResponseResult(CommonCode.SUCCESS,maxNo);
    }

    @PostMapping("ubeStockFinanceapply/retrieveBill")
    public AutoApplyReceiveDTO autoApply(@RequestBody AutoApplyDTO autoApplyDto){
        return remotePoolService.autoApply(autoApplyDto);
    }

}
